var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
//导入日志模块
var logger = require('morgan');
// 导入Swagger-ui-express模块
const swaggerUi = require("swagger-ui-express");
// 导入Swagger-jsdoc模块
const swaggerJsdoc = require("swagger-jsdoc");
//导入初始化数据库中间件
const initSqliteDB = require("./controller/init");

//2.导入路由模块
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
const booksRouter = require("./routes/books");

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
//静态资源
app.use(express.static(path.join(__dirname, 'public')));

// 中间件： Swagger
const options = {
    definition: {
      openapi: "3.0.0",
      info: {
          titile: "我的藏书房接口",
          version: "1.0.0",
      },
    },
    apis: [path.join(__dirname, "./routes/*.js")],
};
   
const openapiSpecification = swaggerJsdoc(options);
app.use("/dev-api", swaggerUi.serve, swaggerUi.setup(openapiSpecification));

//中间件：数据库初始化
app.use(initSqliteDB());

//4.
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/books",booksRouter);

//5.
module.exports = app;
